﻿using System.ComponentModel;
using System.Data;


namespace Marmat.Forms
{
    public partial class BaseGrid
    {

        /// <summary>
        /// 逻辑型，是否允许复制粘贴表中数据。
        /// 此属性默认为True，如果某个表的数据非常重要，可以将此属性设为False，防止数据被非法复制。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [Browsable(false)]
        [DefaultValue(true)]
        public bool AllowClipBoard { get; set; } = true;

        [Browsable(false)]
        public DevExpress.XtraGrid.GridControl BaseControl
        {
            get
            {
                return this.GridControl1;
            }
        }

        /// <summary>
        /// 获取表格中的行数
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [Browsable(false)]
        public int RowCount
        {
            get
            {
                return this.BaseControl.MainView.RowCount;
            }
        }


        /// <summary>
        /// 获取或设置过滤条件表达式。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [Browsable(false)]
        [DefaultValue(null)]
        [Category("杂项")]
        [Description("获取或设置过滤条件表达式。")]
        public string Filter
        {
            get => this._filter;
            set
            {
                this._filter = value;
                this.BindingSource.Filter = value;
            }
        }
        private string _filter;

        /// <summary>
        /// 获取或设置用户控件的边框样式。
        /// </summary>
        /// <returns></returns>
        [Browsable(true)]
        [DefaultValue(typeof(DevExpress.XtraEditors.Controls.BorderStyles), "Default")]
        [Category("外观")]
        [Description("获取或设置用户控件的边框样式。")]
        public new DevExpress.XtraEditors.Controls.BorderStyles BorderStyle
        {
            get
            {
                return this.BaseControl.MainView.BorderStyle;
            }

            set
            {
                this.BaseControl.MainView.BorderStyle = value;
            }
        }

        /// <summary>
        /// 返回或设置当前行位置
        /// </summary>
        [Browsable(false)]
        [DefaultValue(-1)]
        public int Position
        {
            get
            {
                return this.BindingSource.Position;
            }
            set
            {
                this.BindingSource.Position = value;
            }
        }

        /// <summary>
        /// 获取或设置数据行的单元格高度。
        /// </summary>
        /// <returns></returns>
        /// <value>指定数据单元格高度(以像素为单位)的整数值。</value>
        [DefaultValue(-1)]
        public int RowHeight
        {
            get
            {
                return this._rowHeight;
            }

            set
            {
                this._rowHeight = value;
                this.GetMainView().RowHeight = value;
            }
        }

        private int _rowHeight = -1;

        /// <summary>
        /// 获取或设置是否打开分组面板。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(false)]
        [Category("外观")]
        [Description("获取或设置是否打开分组面板。")]
        public bool ShowGroupPanel
        {
            get
            {
                return this._ShowGroupPanel;
            }

            set
            {
                if (this._ShowGroupPanel != value)
                {
                    this._ShowGroupPanel = value;
                    this.SetValue("OptionsView.ShowGroupPanel", value);
                }
            }
        }

        private bool _ShowGroupPanel = false;


        /// <summary>
        /// 返回一个对象，该对象表示网格数据源中的行。
        /// </summary>
        /// <returns></returns>
        public DataRow GetDataRow(int index)
        {
            return this.gridView1.GetDataRow(index);
        }

        /// <summary>
        /// 获取指定行列的值。
        /// </summary>
        /// <param name="index"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public object GetRowCellValue(int index, string columnName)
        {
            return this.gridView1.GetRowCellValue(index, columnName);
        }

        /// <summary>
        /// 设置指定行列的值。
        /// </summary>
        /// <param name="index"></param>
        /// <param name="columnName"></param>
        /// <param name="value"></param>
        public void SetRowCellValue(int index, string columnName, object value)
        {
            this.gridView1.SetRowCellValue(index, columnName, value);
        }

        // Public Sub SetSort(ByVal FieldName As String)
        // Dim list As New List(Of String)
        // If Me.Sort > "" Then
        // list.AddRange(Me.Sort.Split(","))
        // End If
        // If list.Contains(FieldName) Then
        // Me.SetSort(FieldName, "desc")
        // ElseIf list.Contains(FieldName) = False Then
        // Me.SetSort(FieldName, "")
        // End If
        // End Sub

        // Public Sub SetSort(ByVal FieldName As String, ByVal Mode As String)
        // If Mode > "" AndAlso Mode.StartsWith(" ") = False Then
        // Mode = " " & Mode
        // End If
        // Dim Items As New List(Of String)
        // If Me.Sort > "" Then
        // Items.AddRange(Me.Sort.Split(","))
        // End If
        // If Items.Contains(FieldName) Then
        // Items.Item(Items.IndexOf(FieldName)) = FieldName & Mode
        // ElseIf Items.Contains(FieldName & Mode) Then
        // Items.Item(Items.IndexOf(FieldName & Mode)) = FieldName
        // Else
        // Items.Add(FieldName)
        // End If
        // Dim Builder As New System.Text.StringBuilder
        // For Each item As String In Items
        // If item > "" Then
        // If Builder.Length > 0 Then Builder.Append(",")
        // Builder.Append(item)
        // End If
        // Next
        // Me.Sort = Builder.ToString
        // End Sub

        /// <summary>
        /// 获取或设置用于排序的列名称以及用于查看数据源中的行的排序顺序。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(null)]
        public virtual string Sort
        {
            get
            {
                return this.BindingSource.Sort;
            }

            set
            {
                if (!string.Equals(this.Sort, value))
                {
                    var EventArgs = new Views.Grid.BeforeSortEventArgs(this);
                    EventArgs.Sort = value;
                    if (EventArgs.Cancel == false)
                    {
                        this.BindingSource.Sort = value;
                        this.BaseGridView.SortInfo.Clear();
                        if (!string.IsNullOrEmpty(this.Sort))
                        {
                            foreach (string item in this.Sort.Split(','))
                            {
                                if (!string.IsNullOrEmpty(item))
                                {
                                    var Field = item.Split(' ');
                                    if (Field.Length > 0 && this.Columns.Contains(Field[0]))
                                    {
                                        if (Field.Length == 1)
                                        {
                                            this.Columns[Field[0]].GridColumn.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
                                        }
                                        else if (Field.Length == 2)
                                        {
                                            this.Columns[Field[0]].GridColumn.SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
                                        }
                                    }
                                }
                            }
                        }
                        // Dim EventArgs_2 As New  Marmat.Forms.BaseControl.Grid.GridEventArgs(Me)
                        // RaiseEvent AfterSort(Me, EventArgs_2)
                    }
                    // RaiseEvent BeforeSort(Me, EventArgs)
                }
            }
        }

        /// <summary>
        /// 获取或设置连接器绑定到的数据源。
        /// </summary>
        /// <returns></returns>
        [Browsable(false)]
        [DefaultValue(null)]
        public virtual object DataSource
        {
            get
            {
                return this.BindingSource.DataSource;
            }

            set
            {
                this.BindingSource.DataSource = value;
            }
        }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Localizable(true)]
        [NotifyParentProperty(true)]
        [Category("数据")]
        [Description("返回 Table 的列集合")]
        public virtual Views.Grid.ColumnCollection Columns
        {
            get
            {
                if (this._Columns is null)
                {
                    this._Columns = new Views.Grid.ColumnCollection(this);
                }

                return this._Columns;
            }
        }

        private Views.Grid.ColumnCollection _Columns;


        /// <summary>
        /// 返回 DataGrid 的表头集合。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Localizable(true)]
        [NotifyParentProperty(true)]
        [Category("外观")]
        [Description("返回 DataGrid 的表头集合。")]
        public Views.Grid.BandCollection Headers
        {
            get
            {
                if (this._Bands is null)
                {
                    this._Bands = new Views.Grid.BandCollection(this, this.bandedGridView1.Bands);
                }

                return this._Bands;
            }
        }

        private Views.Grid.BandCollection _Bands;

        // Public ReadOnly Property 


        [DefaultValue(typeof(VisualStyleEnum), "GridView")]
        public VisualStyleEnum VisualStyle
        {
            get
            {
                return this._VisualStyle;
            }

            set
            {
                if (this._VisualStyle != value)
                {
                    this._VisualStyle = value;
                    switch (value)
                    {
                        case VisualStyleEnum.GridView:
                            {
                                this.BaseControl.MainView = this.BaseGridView;
                                break;
                            }

                        case VisualStyleEnum.BandedGridView:
                            {
                                this.BaseControl.MainView = this.BaseBandedGridView;
                                break;
                            }

                        case VisualStyleEnum.AdvBandedGridView:
                            {
                                this.BaseControl.MainView = this.BaseAdvBandedGridView;
                                break;
                            }
                    }
                }
            }
        }

        private VisualStyleEnum _VisualStyle = VisualStyleEnum.GridView;

        /// <summary>
        /// 获取表格组件的显示视图。
        /// </summary>
        /// <returns></returns>
        public DevExpress.XtraGrid.Views.Grid.GridView GetMainView()
        {
            return this.GetMainView<DevExpress.XtraGrid.Views.Grid.GridView>();
        }

        /// <summary>
        /// 获取表格组件的显示视图。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetMainView<T>() where T : DevExpress.XtraGrid.Views.Base.BaseView
        {
            return (T)this.BaseControl.MainView;
        }
    }
}